﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rated.DAL.Db;
using Rated.Website.Exceptions;
using System.Collections.Specialized;

namespace Rated.Website.Models
{
    public class RatedItemViewModel
    {
        public RatedItem Item { get; set; }
        public RatedItemViewModel()
        {
            throw new EIncorrectConstructor();
        }

        public RatedItemViewModel(int itemId)
        {


        }

        public static void UpdateDescription(NameValueCollection values)
        {
            var itemId = int.Parse(values["item.ID"]);
            var itemTitle = values["item.Title"];
            var itemDescription = values["item.Description"];
            var db = new RatedEntities();
            var item = db.RatedItems.Where(o => o.ID == itemId).Single();
            item.Title = itemTitle;
            item.Description = itemDescription;
            db.SaveChanges();
        }

        public static RatedItem UpdateLocation(NameValueCollection values)
        {
            var itemId = int.Parse(values["ID"]);
            var continentId = int.Parse(values["ContinentID"]);
            var countryId = 0;
            var localityId = 0;
            var stateId = 0;
            var cityId = 0;
            
            if (continentId > 0)
            {
                countryId = int.Parse(values["CountryID"]);
                if (countryId > 0)
                {
                    localityId = int.Parse(values["LocalityID"]);
                    if (localityId > 0)
                    {
                        stateId = int.Parse(values["StateID"]);
                        if (stateId > 0)
                            cityId = int.Parse(values["CityID"]);
                    }
                }
            }
            var db = new RatedEntities();
            var item = db.RatedItems.Where(o => o.ID == itemId).Single();
            item.ContinentID    = continentId   == 0 ? (int?) null : continentId;
            item.CountryID      = countryId     == 0 ? (int?) null : countryId;
            item.LocalityID     = localityId    == 0 ? (int?) null : localityId;
            item.StateID        = stateId       == 0 ? (int?) null : stateId;
            item.CityID         = cityId        == 0 ? (int?) null : cityId;
            db.SaveChanges();
            return item;
        }

        internal static RatedItem UpdateAttribute(NameValueCollection values)
        {
            var itemId = int.Parse(values["Item.ID"]);
            var attributeId = int.Parse(values["ID"]);
            var selected = values["selected"] == "true";
            var db = new RatedEntities();
            var item = db.RatedItems.Where(o => o.ID == itemId).Single();

            if (selected)
                db.AddToRatedItem_Attribute(new RatedItem_Attribute { RatedItemID = itemId, AttributeID = attributeId });
            else
                db.DeleteObject(item.RatedItem_Attribute.Where(o=>o.AttributeID==attributeId).Single());

            db.SaveChanges();
            return item;
        }

    }
}